//package com.yunxin.core.application.service.impl;
//
//import cn.hutool.core.date.*;
//import cn.hutool.core.util.PageUtil;
//import com.yunxin.common.exception.BusinessException;
//import com.yunxin.common.result.ResultCode;
//import com.yunxin.common.utils.JacksonUtils;
//import com.yunxin.core.application.assembler.ChannelAssembler;
//import com.yunxin.core.application.command.channel.*;
//import com.yunxin.core.application.service.ChannelApplicationAdminService;
//import com.yunxin.core.domain.external.channel.ChannelExternalService;
//import com.yunxin.core.domain.external.channel.ChannelPromotionInfo;
//import com.yunxin.core.domain.model.channel.*;
//import com.yunxin.core.domain.service.channel.ChannelPromotionService;
//import com.yunxin.core.infra.common.channel.enums.PromotionStatusEnum;
//import com.yunxin.core.infra.persistence.mapper.channel.ChannelPromotionExtendMapper;
//import com.yunxin.core.utils.infra.common.constant.CoreResultCode;
//import com.yunxin.core.utils.infra.utils.ChannelFlowRateUtil;
////import com.yunxin.statistics.domain.external.StatisticsExternalService;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Service;
//
//import java.util.Date;
//import java.util.Objects;
//
///**
// * @author zero°
// * @since 2023-07-12
// */
//@Service
//@RequiredArgsConstructor
//public class ChannelApplicationAdminServiceImpl implements ChannelApplicationAdminService {
//
//    private final ChannelInfoRepository channelInfoRepository;
//
//    private final ChannelPromotionRepository channelPromotionRepository;
//
//    private final ChannelPromotionLinkConfigRepository channelPromotionLinkConfigRepository;
//
//    private final ChannelPromotionCustomConfigRepository channelPromotionCustomConfigRepository;
//
//    private final ChannelPromotionUnitPriceConfigRepository channelPromotionUnitPriceConfigRepository;
//
//    private final ChannelPromotionUnitPriceConfigVersionRepository channelPromotionUnitPriceConfigVersionRepository;
//
//    private final  ChannelTypeRepository channelTypeRepository;
//
//    private final  ChannelPromotionApiCustomerConfigRepository channelPromotionApiCustomerConfigRepository;
//
//    private final  ChannelPromotionMerchantConfigRepository channelPromotionMerchantConfigRepository;
//
//    private final ChannelAdvanceChargeRecordRepository channelAdvanceChargeRecordRepository;
//
//    private final ChannelPromotionService channelPromotionService;
//
//    private final ChannelExternalService channelExternalService;
//
//    private final ChannelPromotionExtendMapper channelPromotionExtendMapper;
//
//    private final StatisticsExternalService statisticsExternalService;
//
//    private final ChannelAssembler channelAssembler;
//
//    private final ChannelFlowRateUtil channelFlowRateUtil;
//
//    @Override
//    public boolean addChannel(AddChannelCommand command) {
//        ChannelInfo oldChannelInfo = channelInfoRepository.getChannelInfoByChannelCode(command.getChannelCode());
//        if (Objects.nonNull(oldChannelInfo)) {
//            throw new BusinessException(CoreResultCode.CHANNEL_CODE_EXIST);
//        }
//        ChannelInfo channelInfo = channelAssembler.formAddChannelCommand(command);
//        channelInfo.setEditor(command.getCreator());
//        return channelInfoRepository.saveChannel(channelInfo);
//    }
//
//    @Override
//    public boolean updateChannel(UpdateChannelCommand command) {
//        ChannelInfo oldChannelInfo = channelInfoRepository.getChannelInfoByChannelId(command.getId());
//        if (Objects.isNull(oldChannelInfo)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        ChannelInfo channelInfo = channelAssembler.formUpdateChannelCommand(command);
//        channelInfo.setVersion(oldChannelInfo.getVersion() + 1);
//        return channelInfoRepository.updateChannel(channelInfo);
//    }
//
//    @Override
//    public Long addChannelPromotionBasicInfo(AddChannelPromotionBasicInfoCommand command) {
//        ChannelPromotionInfo result = channelExternalService.getChannelPromotionInfoByPromotionCode(command.getPromotionCode());
//        if (Objects.nonNull(result)) {
//            throw new BusinessException(CoreResultCode.CHANNEL_PROMOTION_CODE_EXIST);
//        }
//        ChannelPromotion channelPromotion = channelAssembler.fromAddChannelPromotionBasicInfoCommand(command);
//        channelPromotion.setEditor(command.getCreator());
//        ChannelPromotionBasicInfo channelPromotionBasicInfo = channelAssembler.fromAddChannelPromotionBasicInfoCommandForBasicInfoCommand(command);
//        channelPromotionBasicInfo.setEditor(command.getCreator());
//        channelPromotionBasicInfo.setProtocolStatus(command.getProtocolStatus());
//        return channelPromotionService.addChannelPromotionBasicInfo(channelPromotion, channelPromotionBasicInfo);
//    }
//
//    @Override
//    public boolean updateChannelPromotionBasicInfo(UpdateChannelPromotionBasicInfoCommand command) {
//        ChannelPromotion oldChannelPromotion = channelPromotionRepository.getChannelPromotionById(command.getPromotionId());
//        if (Objects.isNull(oldChannelPromotion)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        ChannelPromotion channelPromotion = channelAssembler.fromUpdateChannelPromotionBasicInfoCommand(command);
//        // 推广code与原来的不一致，则置为null,不修改推广code
//        if (!oldChannelPromotion.getPromotionCode().equals(channelPromotion.getPromotionCode())) {
//            channelPromotion.setPromotionCode(null);
//        }
//        channelPromotion.setVersion(oldChannelPromotion.getVersion() + 1);
//        ChannelPromotionBasicInfo channelPromotionBasicInfo = channelAssembler.fromUpdateChannelPromotionBasicInfoCommandForBasicInfoCommand(command);
//        channelPromotionBasicInfo.setVersion(channelPromotion.getVersion());
//        channelPromotionBasicInfo.setProtocolStatus(command.getProtocolStatus());
//        return channelPromotionService.updateChannelPromotionBasicInfo(channelPromotion, channelPromotionBasicInfo);
//    }
//
//    @Override
//    public boolean operateChannelPromotionLinkConfig(ChannelPromotionLinkConfigOperateCommand command) {
//        ChannelPromotion channelPromotion = channelPromotionRepository.getChannelPromotionById(command.getPromotionId());
//        if (Objects.isNull(channelPromotion)) {
//            throw new BusinessException(CoreResultCode.CHANNEL_PROMOTION_NOT_EXIST);
//        }
//        ChannelPromotionLinkConfig oldLinkConfig = channelPromotionLinkConfigRepository.getByPromotionId(command.getPromotionId());
//        boolean result;
//        if (Objects.isNull(oldLinkConfig)) {
//            oldLinkConfig = channelAssembler.fromChannelPromotionLinkConfigOperateCommand(command);
//            oldLinkConfig.setCreator(command.getOperator());
//            oldLinkConfig.setEditor(command.getOperator());
//            result = channelPromotionLinkConfigRepository.saveChannelPromotionLinkConfig(oldLinkConfig);
//        } else {
//            ChannelPromotionLinkConfig updateLinkConfig = channelAssembler.fromChannelPromotionLinkConfigOperateCommand(command);
//            updateLinkConfig.setId(oldLinkConfig.getId());
//            updateLinkConfig.setEditor(command.getOperator());
//            updateLinkConfig.setVersion(oldLinkConfig.getVersion() + 1);
//            result = channelPromotionLinkConfigRepository.updateChannelPromotionLinkConfig(updateLinkConfig);
//        }
//        return result;
//    }
//
//    @Override
//    public boolean operateChannelPromotionCustomConfig(ChannelPromotionCustomConfigOperateCommand command) {
//        ChannelPromotion channelPromotion = channelPromotionRepository.getChannelPromotionById(command.getPromotionId());
//        if (Objects.isNull(channelPromotion)) {
//            throw new BusinessException(CoreResultCode.CHANNEL_PROMOTION_NOT_EXIST);
//        }
//        ChannelPromotionCustomConfig oldCustomConfig = channelPromotionCustomConfigRepository.getByPromotionId(command.getPromotionId());
//        boolean result;
//        if (Objects.isNull(oldCustomConfig)) {
//            oldCustomConfig = channelAssembler.fromChannelPromotionCustomConfigOperateCommand(command);
//            oldCustomConfig.setCreator(command.getOperator());
//            oldCustomConfig.setEditor(command.getOperator());
//            result = channelPromotionCustomConfigRepository.saveChannelPromotionCustomConfig(oldCustomConfig);
//        } else {
//            ChannelPromotionCustomConfig updateCustomConfig = channelAssembler.fromChannelPromotionCustomConfigOperateCommand(command);
//            updateCustomConfig.setId(oldCustomConfig.getId());
//            updateCustomConfig.setEditor(command.getOperator());
//            updateCustomConfig.setVersion(oldCustomConfig.getVersion() + 1);
//            result = channelPromotionCustomConfigRepository.updateChannelPromotionCustomConfig(updateCustomConfig);
//        }
//        return result;
//    }
//
//    @Override
//    public boolean operateChannelPromotionUnitPriceConfig(ChannelPromotionUnitPriceConfigOperateCommand command) {
//        ChannelPromotion channelPromotion = channelPromotionRepository.getChannelPromotionById(command.getPromotionId());
//        if (Objects.isNull(channelPromotion)) {
//            throw new BusinessException(CoreResultCode.CHANNEL_PROMOTION_NOT_EXIST);
//        }
//        ChannelPromotionUnitPriceConfig oldUnitPriceConfig = channelPromotionUnitPriceConfigRepository.getByPromotionId(command.getPromotionId());
//        boolean result;
//        if (Objects.isNull(oldUnitPriceConfig)) {
//            oldUnitPriceConfig = channelAssembler.fromChannelPromotionUnitPriceConfigOperateCommand(command);
//            oldUnitPriceConfig.setCreator(command.getOperator());
//            oldUnitPriceConfig.setEditor(command.getOperator());
//            result = channelPromotionUnitPriceConfigRepository.saveChannelPromotionUnitPriceConfig(oldUnitPriceConfig);
//            channelPromotionUnitPriceConfigVersionRepository.saveChannelPromotionUnitPriceConfigVersion(converterChannelPromotionUnitPriceConfigVersion(oldUnitPriceConfig));
//        } else {
//            ChannelPromotionUnitPriceConfig updateUnitPriceConfig = channelAssembler.fromChannelPromotionUnitPriceConfigOperateCommand(command);
//            updateUnitPriceConfig.setId(oldUnitPriceConfig.getId());
//            updateUnitPriceConfig.setEditor(command.getOperator());
//            updateUnitPriceConfig.setVersion(oldUnitPriceConfig.getVersion() + 1);
//            updateUnitPriceConfig.setCurrentVersion(oldUnitPriceConfig.getCurrentVersion() + 1);
//            result = channelPromotionUnitPriceConfigRepository.updateChannelPromotionUnitPriceConfig(updateUnitPriceConfig);
//            channelPromotionUnitPriceConfigVersionRepository.saveChannelPromotionUnitPriceConfigVersion(converterChannelPromotionUnitPriceConfigVersion(updateUnitPriceConfig));
//        }
//        // 价格不一致时通知渠道推广统计变更结算价格
//        if (oldUnitPriceConfig.getChargedPrice().compareTo(command.getChargedPrice()) != 0) {
//            statisticsExternalService.updateChannelPromotionStatisticsSettlementPrice(channelPromotion.getPromotionCode(), new Date(), command.getChargedPrice());
//        }
//        return result;
//    }
//
//    @Override
//    public boolean updateChannelPromotionStatus(UpdateChannelPromotionStatusCommand command) {
//        ChannelPromotion channelPromotion = channelPromotionRepository.getChannelPromotionById(command.getId());
//        if (Objects.isNull(channelPromotion)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        if (PromotionStatusEnum.ENABLE.getValue().equals(command.getPromotionStatus())) {
//            boolean r = channelPromotionRepository.getChannelPromotionSaveResult(command.getId());
//            if (r) {
//                throw new BusinessException(CoreResultCode.PRODUCT_UPDATE_STATUS_ERROR);
//            }
//        }
//        ChannelPromotion updateChannelPromotion = new ChannelPromotion();
//        updateChannelPromotion.setId(command.getId());
//        updateChannelPromotion.setPromotionStatus(command.getPromotionStatus());
//        updateChannelPromotion.setEditor(command.getEditor());
//        updateChannelPromotion.setVersion(channelPromotion.getVersion() + 1);
//        return channelPromotionRepository.updateChannelPromotion(updateChannelPromotion);
//    }
//
//    @Override
//    public boolean addChannelType(AddChannelTypeCommand command) {
//        ChannelType channelType = channelAssembler.formAddChannelTypeCommand(command);
//        channelType.setEditor(command.getCreator());
//        return channelTypeRepository.saveChannelType(channelType);
//    }
//
//    @Override
//    public boolean updateChannelType(UpdateChannelTypeCommand command) {
//        ChannelType oldChannelType = channelTypeRepository.getChannelTypeById(command.getId());
//        if (Objects.isNull(oldChannelType)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        ChannelType channelType = channelAssembler.formUpdateChannelTypeCommand(command);
//        channelType.setVersion(oldChannelType.getVersion() + 1);
//        return channelTypeRepository.updateChannelType(channelType);
//    }
//
//    @Override
//    public boolean relevancePopup(ChannelPopupCommand command) {
//        ChannelPromotion channelPromotion = channelPromotionRepository.getChannelPromotionById(command.getId());
//        if (Objects.isNull(channelPromotion)) {
//            throw new BusinessException(CoreResultCode.CHANNEL_PROMOTION_NOT_EXIST);
//        }
//        ChannelPromotion updateChannelPromotion = new ChannelPromotion();
//        updateChannelPromotion.setId(command.getId());
//        updateChannelPromotion.setPopupCode(command.getPopupCode());
//        updateChannelPromotion.setEditor(command.getEditor());
//        updateChannelPromotion.setVersion(channelPromotion.getVersion() + 1);
//        return channelPromotionRepository.updateChannelPromotion(updateChannelPromotion);
//    }
//
//    @Override
//    public boolean operateChannelPromotionApiCustomerConfig(ChannelPromotionApiCustomerConfigOperateCommand command) {
//        Long id = channelPromotionExtendMapper.getChannelIdByPromotionCode(command.getPromotionCode());
//        if (Objects.isNull(id)) {
//            throw new BusinessException(CoreResultCode.CHANNEL_PROMOTION_NOT_EXIST);
//        }
//
//        Long limitTotal = command.getLimitTotal();
//        Boolean orInit = channelFlowRateUtil.setOrInit(command.getPromotionCode(), limitTotal);
//        ChannelPromotionApiCustomerConfig oldApiCustomerConfig = channelPromotionApiCustomerConfigRepository.getChannelPromotionApiCustomerConfigByPromotionCode(command.getPromotionCode());
//        boolean result;
//        if (Objects.isNull(oldApiCustomerConfig)) {
//            oldApiCustomerConfig = channelAssembler.fromChannelPromotionApiCustomerConfigOperateCommand(command);
//            oldApiCustomerConfig.setCreator(command.getOperator());
//            oldApiCustomerConfig.setLimitTotal(command.getLimitTotal());
//            result = channelPromotionApiCustomerConfigRepository.saveChannelPromotionApiCustomerConfig(oldApiCustomerConfig);
//        } else {
//            ChannelPromotionApiCustomerConfig updateApiCustomerConfig = channelAssembler.fromChannelPromotionApiCustomerConfigOperateCommand(command);
//            updateApiCustomerConfig.setId(oldApiCustomerConfig.getId());
//            updateApiCustomerConfig.setEditor(command.getOperator());
//            updateApiCustomerConfig.setVersion(oldApiCustomerConfig.getVersion() + 1);
//            updateApiCustomerConfig.setLimitTotal(command.getLimitTotal());
//            result = channelPromotionApiCustomerConfigRepository.updateChannelPromotionApiCustomerConfig(updateApiCustomerConfig);
//        }
//
//
//
//
//        return result;
//    }
//
//    @Override
//    public boolean operateChannelPromotionMerchantConfig(ChannelPromotionMerchantConfigOperateCommand command) {
//        ChannelPromotionMerchantConfig channelPromotionMerchantConfig = channelPromotionMerchantConfigRepository.getChannelPromotionMerchantConfigByChannelPromotionCode(command.getChannelPromotionCode());
//        if (Objects.isNull(channelPromotionMerchantConfig)) {
//            channelPromotionMerchantConfig = new ChannelPromotionMerchantConfig();
//            channelPromotionMerchantConfig.setChannelPromotionCode(command.getChannelPromotionCode());
//            if (command.getAppointMerchants() != null && !command.getAppointMerchants().isEmpty()) {
//                channelPromotionMerchantConfig.setAppointMerchants(JacksonUtils.X.format(command.getAppointMerchants()));
//            }
//            channelPromotionMerchantConfig.setCreator(command.getOperator());
//            channelPromotionMerchantConfig.setFilterMerchants(JacksonUtils.X.format(command.getFilterMerchants()));
//            return channelPromotionMerchantConfigRepository.saveChannelPromotionMerchantConfig(channelPromotionMerchantConfig);
//        } else {
//            if (command.getAppointMerchants() != null && !command.getAppointMerchants().isEmpty()) {
//                channelPromotionMerchantConfig.setAppointMerchants(JacksonUtils.X.format(command.getAppointMerchants()));
//            }
//            channelPromotionMerchantConfig.setVersion(channelPromotionMerchantConfig.getVersion() + 1);
//            channelPromotionMerchantConfig.setEditor(command.getOperator());
//            channelPromotionMerchantConfig.setFilterMerchants(JacksonUtils.X.format(command.getFilterMerchants()));
//        }
//        return channelPromotionMerchantConfigRepository.updateChannelPromotionMerchantConfig(channelPromotionMerchantConfig);
//    }
//
//
//    @Override
//    public boolean addChannelAdvanceChargeRecord(AddChannelAdvanceChargeRecordCommand command) {
//        ChannelAdvanceChargeRecord channelAdvanceChargeRecord = channelAssembler.fromAddChannelAdvanceChargeRecordCommand(command);
//        return channelAdvanceChargeRecordRepository.saveChannelAdvanceChargeRecord(channelAdvanceChargeRecord);
//    }
//
//    @Override
//    public boolean updateChannelAdvanceChargeRecord(UpdateChannelAdvanceChargeRecordCommand command) {
//        ChannelAdvanceChargeRecord oldChannelAdvanceChargeRecord = channelAdvanceChargeRecordRepository.getChannelAdvanceChargeRecordById(command.getId());
//        if (Objects.isNull(oldChannelAdvanceChargeRecord)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        ChannelAdvanceChargeRecord channelAdvanceChargeRecord = channelAssembler.fromUpdateChannelAdvanceChargeRecordCommand(command);
//        channelAdvanceChargeRecord.setVersion(oldChannelAdvanceChargeRecord.getVersion() + 1);
//        return channelAdvanceChargeRecordRepository.updateChannelAdvanceChargeRecord(channelAdvanceChargeRecord);
//    }
//
//
//    /**
//     * ChannelPromotionUnitPriceConfig 转换 ChannelPromotionUnitPriceConfigVersion
//     * @param channelPromotionUnitPriceConfig channelPromotionUnitPriceConfig
//     * @return ChannelPromotionUnitPriceConfigVersion
//     */
//    private ChannelPromotionUnitPriceConfigVersion converterChannelPromotionUnitPriceConfigVersion(ChannelPromotionUnitPriceConfig channelPromotionUnitPriceConfig) {
//        ChannelPromotionUnitPriceConfigVersion channelPromotionUnitPriceConfigVersion = new ChannelPromotionUnitPriceConfigVersion();
//        channelPromotionUnitPriceConfigVersion.setPromotionId(channelPromotionUnitPriceConfig.getPromotionId());
//        channelPromotionUnitPriceConfigVersion.setChargedType(channelPromotionUnitPriceConfig.getChargedType());
//        channelPromotionUnitPriceConfigVersion.setChargedPrice(channelPromotionUnitPriceConfig.getChargedPrice());
//        channelPromotionUnitPriceConfigVersion.setChargedWay(channelPromotionUnitPriceConfig.getChargedWay());
//        channelPromotionUnitPriceConfigVersion.setCurrentVersion(channelPromotionUnitPriceConfig.getCurrentVersion());
//        channelPromotionUnitPriceConfigVersion.setTakeEffectTime(channelPromotionUnitPriceConfig.getTakeEffectTime());
//        channelPromotionUnitPriceConfigVersion.setCreator(channelPromotionUnitPriceConfig.getEditor());
//        return channelPromotionUnitPriceConfigVersion;
//    }
//
//}
